package com.hldsmart.smartda.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.activity.home.AssistantActivity;
import com.hldsmart.smartda.activity.home.DiscountActivity;
import com.hldsmart.smartda.activity.home.FootprintActivity;
import com.hldsmart.smartda.activity.home.GroupActivity;
import com.hldsmart.smartda.activity.home.IndividuActivity;
import com.hldsmart.smartda.activity.home.ScanActivity;
import com.hldsmart.smartda.activity.home.SecurityActivity;
import com.hldsmart.smartda.activity.home.SmartActivity;
import com.hldsmart.smartda.adapter.home.HomeAdapter;
import com.hldsmart.smartda.adapter.home.TypeAdapter;
import com.hldsmart.smartda.model.entity.ShopEntity;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 *
 * 首页fragment界面
 */

public class HomeFragment extends Fragment {
    @Bind(R.id.rv_goods)
    RecyclerView recyclerView;
    @Bind(R.id.back_top)
    ImageView back_top;

    @Bind(R.id.ptr_fresh)
    PtrClassicFrameLayout ptrLayout;

    private HomeAdapter adapter;
    private TypeAdapter typeAdapter;
    private GridLayoutManager gridLayout;
    private ArrayList<ShopEntity> shopEntities;




    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new HomeAdapter();
        EventBus.getDefault().register(this);//订阅


    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        recyclerView.setAdapter(adapter);
    }



    @Override
    public void onStart() {
        super.onStart();
        /*当前页面没有数据时,刷新*/
//        if (adapter.getItemCount() <= 2) {
//            ptrLayout.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    ptrLayout.autoRefresh();
//                }
//            }, 200);
//        }
    }


    private void initData(){
        shopEntities=new ArrayList<>();
        shopEntities.add(new ShopEntity("1",R.drawable.ken,"肯德基","特价优惠，全场八折起，超值全家桶，赠送可乐薯条","24.00","48","快餐/外卖"));
        shopEntities.add(new ShopEntity("2",R.drawable.xietupian,"特步女鞋","天天特价秋冬季运动鞋男鞋减震气垫跑步女鞋情侣鞋","89.00","111","女鞋/特步"));
        shopEntities.add(new ShopEntity("3",R.drawable.xin,"星巴克","情人节，特价优惠，全场八折起，第二杯半价哦","68.00","99","饮品/咖啡"));
        shopEntities.add(new ShopEntity("4",R.drawable.shopim,"班尼路服装","特价优惠，全场八折起，提供免费茶水，免费停车位","170.00","288","服装/上衣"));
        shopEntities.add(new ShopEntity("5",R.drawable.ubz,"UBZ女鞋","特价优惠特步女鞋秋冬款名牌运动鞋轻便皮面旅游休闲鞋","68.00","99","女鞋/特步"));
        adapter.addData(shopEntities);
        adapter.notifyDataSetChanged();
    }

    /*RecyclerView和PtrClassicFrameLayout的初始化*/
    private void initView() {

        /*设置recyclerView的manager*/
        gridLayout=new GridLayoutManager(getContext(), 2);
        gridLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0 || position == 1)
                    return 2;
                return 2;
            }
        });
        initData();
        recyclerView.setLayoutManager(gridLayout);
        /*设置refreshLayout*/
        /*使用本对象作为key，来记录上一次刷新时间，如果两次下拉间隔太近，不会触发刷新方法*/
        ptrLayout.setLastUpdateTimeRelateObject(this);
        /*关闭Header所耗时长*/
        ptrLayout.setDurationToCloseHeader(1500);

        ptrLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                // 加载更多

            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // 下拉刷新
                initData();
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (gridLayout.findFirstVisibleItemPosition()<=0){
                    back_top.setVisibility(View.GONE);
                }else {
                    back_top.setVisibility(View.VISIBLE);
                }
            }
        });
        back_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerView.scrollToPosition(0);
            }
        });

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void evenStart(Integer position){
        switch (position){
            case 0:
                SecurityActivity.startSecurityActivity(getActivity());
                break;
            case 1:
                AssistantActivity.startAssistantActivity(getActivity());
                break;
            case 2:
                FootprintActivity.startFootprintActivity(getActivity());
                break;
            case 3:
                ScanActivity.startScanActivity(getActivity());
                break;
            case 4:
                SmartActivity.startSmartActivity(getActivity());
                break;
            case 5:
                IndividuActivity.startIndividuActivity(getActivity());
                break;
            case 6:
                GroupActivity.startGroupActivity(getActivity());
                break;
            case 7:
                DiscountActivity.startDiscountActivity(getActivity());
                break;
                }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//取消订阅
    }
}
